import { ValueT } from '@/type';
import { Color } from '@/utils/CanvasUtils/CanvasType';
import { WatchViewI } from '../type';
import CanvasUtils from '@/utils/CanvasUtils';

/** 绘制文本参数 */
export interface StringStyle {
	/** x位置 */
	x: ValueT<number>;
	/** y位置 */
	y: ValueT<number>;
	/** 绘制的文本 */
	content: ValueT<string>;
	/** 文本大小 */
	size?: ValueT<number>;
	/** 边框颜色 */
	borderColor?: ValueT<Color>;
	/** 文字颜色 */
	color: ValueT<Color>;
	/** 左右对齐 */
	textAlign?: ValueT<CanvasTextAlign>;
	/** 文本基线 */
	baseLine?: ValueT<CanvasTextBaseline>;
	/** 线条宽度 */
	lineWidth?: ValueT<number>;
	/** 透明度 */
	transparency?: ValueT<number>;
	/** 是否显示 */
	show?: ValueT<boolean>;
}

/** 文本绘制类 */
export default class StringView implements WatchViewI {

	public Z_Index: ValueT<number>;

	/** 画板类 */
	public canvas: CanvasUtils;

	/** 绘制文本参数 */
	private save: StringStyle;

	/**
	 * 构造函数
	 * @param canvas 画板类
	 * @param save 绘制文本参数
	 * @param shwo 是否显示
	 * @param zIndex 绘制级别(最大最上)
	 */
	constructor(canvas: CanvasUtils, save: StringStyle, zIndex?: ValueT<number>) {
		this.canvas = canvas;
		this.save = save;
		this.Z_Index = zIndex || { Value: 0 };
	}

	/** @override */
	public Draw() {
		const { canvas, save } = this;
		const { x, y, size, content, textAlign, baseLine, color, borderColor, lineWidth, transparency, show } = save;
		if (show && !show.Value) { return; }
		const contentV = content.Value;
		canvas.Save();
		if (transparency) {
			const transparencyV = transparency.Value;
			canvas.GlobalAlpha = transparencyV;
		}
		canvas.Translate(x.Value, y.Value);
		if (textAlign) { canvas.TextAlign = textAlign.Value; }
		if (baseLine) { canvas.TextBaseline = baseLine.Value; }
		if (size) { canvas.FontSize = size.Value; }
		if (borderColor) {
			canvas.StrokeStyle = borderColor.Value;
			if (lineWidth) {
				canvas.LineWidth = lineWidth.Value;
			}
			canvas.StrokeText({ Spot: { X: 0, Y: 0 }, Text: contentV });
		}
		canvas.FillStyle = color.Value;
		canvas.FillText({ Spot: { X: 0, Y: 0 }, Text: contentV });
		canvas.Restore();
	}

}
